<!DOCTYPE html>
<html lang="en">
    <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <meta content="Bmob,BmobCloud,bomb,BaaS,mBaaS,PaaS,Serverless,FaaS,Function as a Service,Backend as a Service,serverless computing,cloud function,后端云,bmob后端云,小程序云,小程序后端,云数据库,云存储,文件存储,云函数,云端代码,定时任务,游戏后端,游戏云,用户系统,无服务器函数,移动开发,app开发,小程序开发,云端一体化,互联网中间件" name="keywords"/>
            <meta content="国内首家专注于移动应用Serverless云服务的平台,Bmob后端云让移动开发更简单,全方位一体化的后端服务平台" name="description"/>
            
            
            
            <link rel="shortcut icon" href="../../../img/favicon.ico">
        
        <script type="text/javascript" src="//game.bmob.cn/static/doc_union.js"></script>
            <!--
            <title>Swift快速入门 - Bmob文档中心</title>
            -->
            <title>数据存储 &middot; iOS – Bmob后端云 </title>        
            
            <link href="../../../css/bootstrap-custom.min.css" rel="stylesheet">
            <link href="../../../css/font-awesome-4.5.0.css" rel="stylesheet">
            <link href="../../../css/base.css" rel="stylesheet">
            <link rel="stylesheet" href="../../../css/highlight.css">
            <link href="../../../css/agate.css" rel="stylesheet">
            <link href="../../../css/custom.css" rel="stylesheet" id="custom">
    </head>

    <body >

        <div class="navbar navbar-fixed-top" role="navigation">
    <div class="main-nav">
        <!-- Collapsed navigation -->
        <div class="navbar-header">
            <a class="navbar-brand" href="../../..">
                <!--Bmob文档中心-->
                <img src="../../../img/logo.png" alt="">
            </a>
        </div>

        <!-- Expanded navigation -->
        <div class="navbar-collapse collapse">

          <ul class="nav navbar-nav">
            
              <li class="pull-left">
                
					<a href="../../..">文档首页</a>
                
              </li>
            
              <li class="pull-left active">
                
					<!--<a href="">数据服务</a>-->
					<li class="dropdown active">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">数据服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
								<li >
									<a href="../../android/">Android</a>
                                </li>
                            
                            
								<li class="active">
									<a href="../">iOS</a>
                                </li>
                            
                            
								<li >
									<a href="../../csharp/">C#</a>
                                </li>
                            
                            
								<li >
									<a href="../../php/">PHP</a>
                                </li>
                            
                            
								<li >
									<a href="../../go/">GO</a>
                                </li>
                            
                            
								<li >
									<a href="../../restful/">REST API</a>
                                </li>
                            
                            
								<li >
									<a href="../../wechat_app_new/rm/">JavaScript</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../../cocos2d_x/">Cocos2D-X</a>
                                </li>
							
                            
								<li >
									<a href="../../wechat_app/">小程序</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../../wechat_app_new/">快应用</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../wechat_app_new/">Nodejs</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../wechat_app_new/">Cocos Creator</a>
                                </li>
							
                            
								<li >
									<a href="../../wechat_app_new/rm/">小程序(新)</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../../kotlin/">Kotlin</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../python/">Python</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../../cloud_function/android/">云函数</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">云函数 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../../cloud_function/android/">Android</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../cloud_function/ios/">iOS</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../cloud_function/java/">Java</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../cloud_function/python/">Python</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../cloud_function/csharp/">C#</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../cloud_function/php/">PHP</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../cloud_function/javascript/">JavaScript</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../cloud_function/restful/">REST API</a>
                                </li>
							
                            
								<li >
									<a href="../../../cloud_function/web/">Web</a>
                                </li>
                            
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../../sms/android/">短信服务</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">短信服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../../sms/android/">Android</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../sms/ios/">iOS</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../sms/php/">PHP</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../sms/javascript/">JavaScript</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../sms/restful/">REST API</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="">游戏实时后端</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">游戏实时后端 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
								<li >
									<a href="../../../game/unity/quick_start/">Unity</a>
                                </li>
                            
                            
								<li >
									<a href="../../../game/cocos_creator/quick_start/">Cocos Creator</a>
                                </li>
                            
                            
								<li >
									<a href="../../../game/wechat_games/quick_start/">微信小游戏</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../../../game/cloud_function/develop_doc/">云函数</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../game/classic_case/">经典案例</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="">即时通讯</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">即时通讯 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
								<li >
									<a href="../../../im/android/">Android</a>
                                </li>
                            
                            
								<li >
									<a href="../../../im/ios/">iOS</a>
                                </li>
                            
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../../push/android/">推送服务</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">推送服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../../push/android/">Android</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../push/ios/">iOS</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../push/php/">PHP</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../push/javascript/">JavaScript</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../push/restful/">REST API</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../../other/common_problem/">其他</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">其他 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../../other/common_problem/">常见问题</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../other/error_code/">错误码</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../other/data_safety/">数据安全</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../../other/bql/">BQL</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li>
                  <a href="https://www.bmob.cn/repository/index" target="_blank">知识库</a>
              </li>
              <li>
                  <a href="http://doc.bmob.cn/video/index.html" target="_blank">视频教程</a>
              </li>
            </ul>
            
        </div>
    </div>
</div>

        <div class="pagebody" id="main-wrapper">
            <div class="sidebar">
                <div class="bs-sidebar hidden-print affix well" role="complementary">

	
		
	
		
				
				
					<div class="code-title">iOS</div>
				
				
				
				
				
				
				
				
				
				
				
				
				
				
		
	
		
				
				
				
				
				
				
				
				
				
		
	
		
				
				
				
				
				
		
	
		
				
				
				
				
				
		
	
		
				
				
		
	
		
				
				
				
				
				
		
	
		
				
				
				
				
		
	


	<ul class="nav bs-sidenav">
		
			
		
			
				
					
						
					
						
							
								
									<li class="">
										<a class="itm-l1" href="../">快速入门</a>
										
									</li>
								
									<li class="">
										<a class="itm-l1" href="../develop_doc/">开发文档</a>
										
									</li>
								
									<li class="">
										<a class="itm-l1" href="../example/">示例/功能</a>
										
									</li>
								
									<li class="">
										<a class="itm-l1" href="../update_log/">更新日志</a>
										
									</li>
								
									<li class="">
										<a class="itm-l1" href="../classdoc/">类库文档</a>
										
									</li>
								
									<li class="">
										<a class="itm-l1" href="../lib_gdx/">libGDX</a>
										
									</li>
								
									<li class="active">
										<a class="itm-l1" href="./">Swift快速入门</a>
										
										<ul class="nav">
											
											<li class="active "><a href="#bmob">在Bmob上创建应用</a>
												
											</li>
												
											<li class=""><a href="#swift">创建Swift项目</a>
												
											</li>
												
											<li class=""><a href="#bmobsdk">设置BmobSDK</a>
												
											</li>
												
											<li class=""><a href="#_1">创建桥接头文件</a>
												
											</li>
												
											<li class=""><a href="#curd">测试CURD功能</a>
												
											</li>
												
											<li class=""><a href="#_2">案例源码</a>
												
											</li>
												
										</ul>
										
									</li>
								
									<li class="">
										<a class="itm-l1" href="../swift_develop_doc/">Swift开发文档</a>
										
									</li>
								
							
						
					
						
					
						
					
						
					
						
					
						
					
						
					
						
					
						
					
						
					
						
					
						
					
						
					
						
					
				
			
		
			
		
			
		
			
		
			
		
			
		
			
		
	</ul>
</div>
            </div>
            <div class="content" role="main">
                <div class="wrap">
                    

<p>本文档的目的是为了方便大家在Swift工程中使用BmobSDK，实现应用的快速开发。下面介绍怎么在 Swift 工程中使用 BmobSDK。</p>
<h2 id="bmob">在Bmob上创建应用<a class="headerlink" href="#bmob" title="Permanent link">&para;</a></h2>
<p>关于如何创建应用，具体请参照<a href="https://docs.bmob.cn/data/iOS/a_faststart/doc/index.html">快速入门</a></p>
<h2 id="swift">创建Swift项目<a class="headerlink" href="#swift" title="Permanent link">&para;</a></h2>
<p>使用Xcode6创建一个新的Single View Application</p>
<p><img alt="" src="../image/Swift_BmobSDK_01.png" /></p>
<p>Language选择Swift</p>
<p><img alt="" src="../image/Swift_BmobSDK_02.png" /></p>
<h2 id="bmobsdk">设置BmobSDK<a class="headerlink" href="#bmobsdk" title="Permanent link">&para;</a></h2>
<p>从官网下载最新的<a href="https://bmob.cn/sdk/Bmob_iOS_SDK_v2.3.0-20180411.zip">BmobSDK</a>,解压导入项目。</p>
<p>添加依赖库文件:</p>
<ul>
<li>Foundation.framework</li>
<li>CoreLocation.framework</li>
<li>Security.framework</li>
<li>CoreGraphics.framework</li>
<li>MobileCoreServices.framework</li>
<li>CFNetwork.framework</li>
<li>CoreTelephony.framework</li>
<li>SystemConfiguration.framework</li>
<li>AVFoundation.framework</li>
<li>MediaPlayer.framework</li>
<li>libz.1.2.5.tbd</li>
<li>libicucore.tbd</li>
<li>libsqlite3.tbd</li>
<li>libc++.tbd</li>
<li>libWeChatSDK.a(如果需要使用支付功能，必须导入，可从微信开放平台下载最新的)</li>
<li>photos.framework</li>
</ul>
<p>添加完成后，应该像这个样子</p>
<p><img alt="" src="../image/Swift_BmobSDK_03.png" /></p>
<p>或者开发者也可以通过cocoapods来进行管理
Podfile 文件的内容可以写成</p>
<pre><code>target &quot;xxxxxx&quot; do
platform:ios,&quot;8.0&quot;
use_frameworks!

pod 'BmobSDK'


end

</code></pre>

<h2 id="_1">创建桥接头文件<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h2>
<p>想要在Swift中使用Objective-C 的类和方法的话，需要创建一个.h 头文件，把你想在 Swift 中使用的 Objective-C 的头文件都包含进来。创建桥接头文件的方法有两种：可以自己手动创建一个桥接头文件并在项目配置项里面进行设置，也可以使用更快捷的方式，在你的项目里创建一个无用的 Objective-C 类文件（如：test.m），Xcode 将询问你是否要创建一个桥接头文件：</p>
<p><img alt="" src="../image/Swift_BmobSDK_04.png" /></p>
<p>完成之后，你就可以删除test.m文件了,然后在 BmobSwift-Bridging-Header.h 中引入</p>
<pre><code>#import &lt;BmobSDK/Bmob.h&gt;
</code></pre>

<h2 id="curd">测试CURD功能<a class="headerlink" href="#curd" title="Permanent link">&para;</a></h2>
<p>在AppDelegate.swift注册申请的AppKey</p>
<pre><code>    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -&gt; Bool {
        // Override point for customization after application launch.

        Bmob.register(WithAppKey: &quot;xxxxx&quot;)

        return true
    }
</code></pre>

<p>在ViewController.swift 中添加函数</p>
<pre><code>//创建方法
    func save(){
        let gamescore:BmobObject = BmobObject(className: &quot;GameScore&quot;)
        gamescore.setObject(&quot;Jhon Smith&quot;, forKey: &quot;playerName&quot;)
        gamescore.setObject(90, forKey: &quot;score&quot;)
        gamescore.saveInBackgroundWithResultBlock { (isSuccessful, error) in
            if error != nil{
                print(&quot;error is \(error.localizedDescription)&quot;)
            }else{
                print(&quot;success&quot;)
            }
        }
    }
</code></pre>

<p>然后在viewDidLoad函数中调用</p>
<pre><code>override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        save()
    }
</code></pre>

<p>现在就可以在WEB后台查看是否创建成功，如下图所示。</p>
<p><img alt="" src="../image/Swift_BmobSDK_05.png" /></p>
<pre><code>//查询方法
func queryUsers()  {
        let query:BmobQuery = BmobUser.query()
        query.orderByDescending(&quot;createdAt&quot;)
        query.findObjectsInBackgroundWithBlock { (array, error) in
            for i in 0..&lt;array.count{
                let obj : BmobUser = array[i] as! BmobUser
                print(&quot;object id \(obj.objectId),username \(obj.username)&quot;)

            }
        }
    }

</code></pre>

<pre><code>//更新方法
func update() {
        let  gamescore:BmobObject = BmobObject(outDatatWithClassName: &quot;GameScore&quot;, objectId: &quot;f3a82207ed&quot;)
        gamescore.setObject(91, forKey: &quot;score&quot;)
        gamescore.updateInBackgroundWithResultBlock { (isSuccessful, error) in
            if error != nil{
                print(&quot;error is \(error.localizedDescription)&quot;)
            }else{
                print(&quot;success&quot;)
            }
        }
    }
</code></pre>

<pre><code>//删除方法
    func deleteGameScore()  {
        let  gamescore:BmobObject = BmobObject(outDatatWithClassName: &quot;GameScore&quot;, objectId: &quot;4faf28f4dd&quot;)
        gamescore.deleteInBackgroundWithBlock { (isSuccessful, error) in
            if error != nil{
                print(&quot;error is \(error.localizedDescription)&quot;)
            }else{
                print(&quot;success&quot;)
            }
        }
    }
</code></pre>

<h2 id="_2">案例源码<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h2>
<p><a href="https://github.com/bmob/bmob-ios-demo/blob/master/SwiftDemo.zip" title="点击下载源码">点击下载源码</a></p>
                </div>
            </div>
        </div>

        
            <script>var base_url = '../../..';</script>
            <script src="../../../js/jquery-1.10.2.min.js"></script>
            <script src="../../../js/bootstrap-3.0.3.min.js"></script>
            <script src="../../../js/highlight.pack.js"></script>
            <script src="../../../js/main.js"></script>
            <script src="../../../js/base.js"></script>

        <div id="go-top"><i class="fa fa-chevron-up"></i></div>
        <a href="https://docs.bmob.cn/data/Android/a_faststart/doc/index.html" id="back" target="_blank">返回 <br> 旧版</a>
    </body>

</html>